<?php

namespace App\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Exceptions\CustomException;

class Base extends Command
{
    const PREFIX = 'uniform:';

    /**
     * Execute the console command.
     *
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
     * @return mixed
     */
    protected function execute($input, $output)
    {
        $args = $this->arguments();
        $options = $this->options();
        $options = array_splice($options, 0, count($options) - 8);
        unset($args['command']);
        $this->info('==BEGIN==' . $this->name . '====:', array_merge($args, $options));

        try {
            $ret = $this->laravel->call([$this, 'handle']);
        } catch (CustomException $e) {
            return $this->error("Error Code:" . $e->getCode() . ", Msg:" . $e->getMessage());
        } catch (\Exception $e) {
            return Log::alert($e);
        }

        $this->info('==END==' . $this->name . "====");
        return $ret;
    }

    public function info($msg, $context = [])
    {
        parent::info(date('Y-m-d H:i:s') . ' ' . $msg . (!empty($context) ? json_encode($context) : ''));
        Log::info($msg, $context);
    }

    public function error($msg, $context = [])
    {
        parent::error(date('Y-m-d H:i:s') . ' ' . $msg . (!empty($context) ? json_encode($context) : ''));
        Log::error($msg, $context);
    }
}
